package key.w8.boot.util;

import com.k8.common.util.StrUtil;
import com.k8.registry.util.RegistryUtil;
import com.k8.rpc.config.RegistryConfig;
import com.k8.rpc.url.URL;
import key.w8.boot.properties.Registry;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: k8
 * @CreateTime: 2025-08-17
 * @Version: 1.0
 */
public class ConvertURLUtil {
    public static List<URL> getRegistryConfigUrls(List<Registry> registries, URL systemConfigUrl, String side) {
        return registries.stream()
                .map(registry -> {
                    if (StrUtil.isEmpty(registry.getHost()) || registry.getPort() == null || StrUtil.isEmpty(registry.getName())) throw new IllegalStateException("Host or port error.");
                    RegistryConfig registryConfig = new RegistryConfig();
                    registryConfig.setHost(registry.getHost());
                    registryConfig.setPort(registry.getPort());
                    registryConfig.setRegistryName(registry.getName());
                    return registryConfig;
                }).map(registryConfig1 -> {
                    return RegistryUtil.buildUrlByRegistryConfig(registryConfig1, systemConfigUrl, side);
                }).collect(Collectors.toList());
    }
}
